using System;
using System.ComponentModel;
using System.Data;
using System.Security.Permissions;
using Microsoft.SharePoint.Security;

namespace PS.WebParts.Rollup.FP
{
    [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
    public sealed class RollupViewResultItem : ICustomTypeDescriptor
    {
        #region Nested Types

        //internal class PropertyName
        //{
        //    // Methods
        //    internal PropertyName(string name)
        //    {
        //        this.name = name;
        //    }

        //    // Properties
        //    internal string Name { get; }
        //    internal string NameLower { get; }

        //    // Fields
        //    private string name;
        //    private string nameLower;
        //}

        //internal class SPListItemSystemProperties
        //{
        //    // Methods
        //    static SPListItemSystemProperties();
        //    public SPListItemSystemProperties();

        //    // Fields
        //    public static readonly SPDataSourceViewPropertyDescriptor Attachments;
        //    public static readonly SPDataSourceViewPropertyDescriptor Audit;
        //    public static readonly SPDataSourceViewPropertyDescriptor CopyDestinations;
        //    public static readonly SPDataSourceViewPropertyDescriptor CopyFieldMask;
        //    public static readonly SPDataSourceViewPropertyDescriptor CopySource;
        //    public static Dictionary<string, SPDataSourceViewPropertyDescriptor> DescriptorDictionary;
        //    public static readonly SPDataSourceViewPropertyDescriptor EffectiveBasePermissions;
        //    public static readonly SPDataSourceViewPropertyDescriptor Fields;
        //    public static readonly SPDataSourceViewPropertyDescriptor File;
        //    public static readonly SPDataSourceViewPropertyDescriptor FileSystemObjectType;
        //    public static readonly SPDataSourceViewPropertyDescriptor FirstUniqueAncestor;
        //    public static readonly SPDataSourceViewPropertyDescriptor Folder;
        //    public static readonly SPDataSourceViewPropertyDescriptor HasAttachments;
        //    public static readonly SPDataSourceViewPropertyDescriptor HasFile;
        //    public static readonly SPDataSourceViewPropertyDescriptor HasUniqueRoleAssignments;
        //    public static readonly SPDataSourceViewPropertyDescriptor ID;
        //    public static readonly SPDataSourceViewPropertyDescriptor Index;
        //    public static readonly SPDataSourceViewPropertyDescriptor IsNew;
        //    public static readonly SPDataSourceViewPropertyDescriptor ListItems;
        //    public static readonly SPDataSourceViewPropertyDescriptor ModerationInformation;
        //    public static readonly SPDataSourceViewPropertyDescriptor Name;
        //    public static readonly SPDataSourceViewPropertyDescriptor ParentList;
        //    public static readonly SPDataSourceViewPropertyDescriptor Properties;
        //    public static readonly SPDataSourceViewPropertyDescriptor RecurrenceID;
        //    public static readonly SPDataSourceViewPropertyDescriptor ReusableAcl;
        //    public static readonly SPDataSourceViewPropertyDescriptor RoleAssignments;
        //    public static readonly SPDataSourceViewPropertyDescriptor Title;
        //    public static readonly SPDataSourceViewPropertyDescriptor ToDoTasks;
        //    public static readonly SPDataSourceViewPropertyDescriptor UniqueId;
        //    public static readonly SPDataSourceViewPropertyDescriptor Url;
        //    public static readonly SPDataSourceViewPropertyDescriptor Versions;
        //    public static readonly SPDataSourceViewPropertyDescriptor Web;
        //    public static readonly SPDataSourceViewPropertyDescriptor Workflows;
        //    public static readonly SPDataSourceViewPropertyDescriptor Xml;
        //}

        //internal class SPListSystemProperties
        //{
        //    // Methods
        //    static SPListSystemProperties();
        //    public SPListSystemProperties();

        //    // Fields
        //    public static readonly SPDataSourceViewPropertyDescriptor AlertTemplate;
        //    public static readonly SPDataSourceViewPropertyDescriptor AllowContentTypes;
        //    public static readonly SPDataSourceViewPropertyDescriptor AllowDeletion;
        //    public static readonly SPDataSourceViewPropertyDescriptor AllowMultiResponses;
        //    public static readonly SPDataSourceViewPropertyDescriptor AnonymousPermMask;
        //    public static readonly SPDataSourceViewPropertyDescriptor AnonymousPermMask64;
        //    public static readonly SPDataSourceViewPropertyDescriptor Audit;
        //    public static readonly SPDataSourceViewPropertyDescriptor Author;
        //    public static readonly SPDataSourceViewPropertyDescriptor BaseTemplate;
        //    public static readonly SPDataSourceViewPropertyDescriptor BaseType;
        //    public static readonly SPDataSourceViewPropertyDescriptor CanReceiveEmail;
        //    public static readonly SPDataSourceViewPropertyDescriptor ContentTypes;
        //    public static readonly SPDataSourceViewPropertyDescriptor ContentTypesEnabled;
        //    public static readonly SPDataSourceViewPropertyDescriptor Created;
        //    public static readonly SPDataSourceViewPropertyDescriptor CurrentChangeToken;
        //    public static readonly SPDataSourceViewPropertyDescriptor DefaultView;
        //    public static readonly SPDataSourceViewPropertyDescriptor DefaultViewUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor Description;
        //    public static Dictionary<string, SPDataSourceViewPropertyDescriptor> DescriptorDictionary;
        //    public static readonly SPDataSourceViewPropertyDescriptor Direction;
        //    public static readonly SPDataSourceViewPropertyDescriptor DraftVersionVisibility;
        //    public static readonly SPDataSourceViewPropertyDescriptor EffectiveBasePermissions;
        //    public static readonly SPDataSourceViewPropertyDescriptor EmailAlias;
        //    public static readonly SPDataSourceViewPropertyDescriptor EnableAssignToEmail;
        //    public static readonly SPDataSourceViewPropertyDescriptor EnableAttachments;
        //    public static readonly SPDataSourceViewPropertyDescriptor EnableMinorVersions;
        //    public static readonly SPDataSourceViewPropertyDescriptor EnableModeration;
        //    public static readonly SPDataSourceViewPropertyDescriptor EnableSyndication;
        //    public static readonly SPDataSourceViewPropertyDescriptor EnableVersioning;
        //    public static readonly SPDataSourceViewPropertyDescriptor EventReceivers;
        //    public static readonly SPDataSourceViewPropertyDescriptor EventSinkAssembly;
        //    public static readonly SPDataSourceViewPropertyDescriptor EventSinkClass;
        //    public static readonly SPDataSourceViewPropertyDescriptor EventSinkData;
        //    public static readonly SPDataSourceViewPropertyDescriptor ExcludeFromTemplate;
        //    public static readonly SPDataSourceViewPropertyDescriptor Fields;
        //    public static readonly SPDataSourceViewPropertyDescriptor FirstUniqueAncestor;
        //    public static readonly SPDataSourceViewPropertyDescriptor Flags;
        //    public static readonly SPDataSourceViewPropertyDescriptor ForceCheckout;
        //    public static readonly SPDataSourceViewPropertyDescriptor Forms;
        //    public static readonly SPDataSourceViewPropertyDescriptor HasExternalEmailHandler;
        //    public static readonly SPDataSourceViewPropertyDescriptor HasUniqueRoleAssignments;
        //    public static readonly SPDataSourceViewPropertyDescriptor Hidden;
        //    public static readonly SPDataSourceViewPropertyDescriptor ID;
        //    public static readonly SPDataSourceViewPropertyDescriptor ImageUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor InternalName;
        //    public static readonly SPDataSourceViewPropertyDescriptor ItemCount;
        //    public static readonly SPDataSourceViewPropertyDescriptor Items;
        //    public static readonly SPDataSourceViewPropertyDescriptor LastItemDeletedDate;
        //    public static readonly SPDataSourceViewPropertyDescriptor LastItemModifiedDate;
        //    public static readonly SPDataSourceViewPropertyDescriptor Lists;
        //    public static readonly SPDataSourceViewPropertyDescriptor MultipleDataList;
        //    public static readonly SPDataSourceViewPropertyDescriptor OnQuickLaunch;
        //    public static readonly SPDataSourceViewPropertyDescriptor Ordered;
        //    public static readonly SPDataSourceViewPropertyDescriptor ParentWeb;
        //    public static readonly SPDataSourceViewPropertyDescriptor ParentWebUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor Permissions;
        //    public static readonly SPDataSourceViewPropertyDescriptor Properties;
        //    public static readonly SPDataSourceViewPropertyDescriptor PropertiesXml;
        //    public static readonly SPDataSourceViewPropertyDescriptor ReadSecurity;
        //    public static readonly SPDataSourceViewPropertyDescriptor ReusableAcl;
        //    public static readonly SPDataSourceViewPropertyDescriptor RoleAssignments;
        //    public static readonly SPDataSourceViewPropertyDescriptor RootFolder;
        //    public static readonly SPDataSourceViewPropertyDescriptor RootFolderUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor SchemaXml;
        //    public static readonly SPDataSourceViewPropertyDescriptor SendToLocationName;
        //    public static readonly SPDataSourceViewPropertyDescriptor SendToLocationUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor ShowUser;
        //    public static readonly SPDataSourceViewPropertyDescriptor Title;
        //    public static readonly SPDataSourceViewPropertyDescriptor Version;
        //    public static readonly SPDataSourceViewPropertyDescriptor Views;
        //    public static readonly SPDataSourceViewPropertyDescriptor WorkflowAssociations;
        //    public static readonly SPDataSourceViewPropertyDescriptor WriteSecurity;
        //}

        //internal class SPWebSystemProperties
        //{
        //    // Methods
        //    static SPWebSystemProperties();
        //    public SPWebSystemProperties();

        //    // Fields
        //    public static readonly SPDataSourceViewPropertyDescriptor Alerts;
        //    public static readonly SPDataSourceViewPropertyDescriptor AllowAnonymousAccess;
        //    public static readonly SPDataSourceViewPropertyDescriptor AllowGlobalCatalog;
        //    public static readonly SPDataSourceViewPropertyDescriptor AllowP2P;
        //    public static readonly SPDataSourceViewPropertyDescriptor AllProperties;
        //    public static readonly SPDataSourceViewPropertyDescriptor AllUsers;
        //    public static readonly SPDataSourceViewPropertyDescriptor AlternateCssUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor AlternateHeader;
        //    public static readonly SPDataSourceViewPropertyDescriptor AnonymousPermMask64;
        //    public static readonly SPDataSourceViewPropertyDescriptor AnonymousState;
        //    public static readonly SPDataSourceViewPropertyDescriptor Audit;
        //    public static readonly SPDataSourceViewPropertyDescriptor AuthenticationMode;
        //    public static readonly SPDataSourceViewPropertyDescriptor Author;
        //    public static readonly SPDataSourceViewPropertyDescriptor AvailableContentTypes;
        //    public static readonly SPDataSourceViewPropertyDescriptor AvailableFields;
        //    public static readonly SPDataSourceViewPropertyDescriptor CanAddDeleteWebParts;
        //    public static readonly SPDataSourceViewPropertyDescriptor CanCustomizePages;
        //    public static readonly SPDataSourceViewPropertyDescriptor CanPersonalizeWebParts;
        //    public static readonly SPDataSourceViewPropertyDescriptor Configuration;
        //    public static readonly SPDataSourceViewPropertyDescriptor ContentTypes;
        //    public static readonly SPDataSourceViewPropertyDescriptor Context;
        //    public static readonly SPDataSourceViewPropertyDescriptor Created;
        //    public static readonly SPDataSourceViewPropertyDescriptor CurrencyLocaleID;
        //    public static readonly SPDataSourceViewPropertyDescriptor CurrentChangeToken;
        //    public static readonly SPDataSourceViewPropertyDescriptor CurrentUser;
        //    public static readonly SPDataSourceViewPropertyDescriptor CustomizationMode;
        //    public static readonly SPDataSourceViewPropertyDescriptor CustomJSUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor CustomMasterUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor DataRetrievalServicesSettings;
        //    public static readonly SPDataSourceViewPropertyDescriptor DefaultUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor DefaultView;
        //    public static readonly SPDataSourceViewPropertyDescriptor Description;
        //    public static Dictionary<string, SPDataSourceViewPropertyDescriptor> DescriptorDictionary;
        //    public static readonly SPDataSourceViewPropertyDescriptor DocTemplates;
        //    public static readonly SPDataSourceViewPropertyDescriptor EffectiveBasePermissions;
        //    public static readonly SPDataSourceViewPropertyDescriptor EffectivePresenceEnabled;
        //    public static readonly SPDataSourceViewPropertyDescriptor EmailInsertsEnabled;
        //    public static readonly SPDataSourceViewPropertyDescriptor EventHandlersEnabled;
        //    public static readonly SPDataSourceViewPropertyDescriptor EventReceivers;
        //    public static readonly SPDataSourceViewPropertyDescriptor ExecuteUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor Exists;
        //    public static readonly SPDataSourceViewPropertyDescriptor ExternalSecurityProviderSetting;
        //    public static readonly SPDataSourceViewPropertyDescriptor Features;
        //    public static readonly SPDataSourceViewPropertyDescriptor Fields;
        //    public static readonly SPDataSourceViewPropertyDescriptor FieldTypeDefinitionCollection;
        //    public static readonly SPDataSourceViewPropertyDescriptor Files;
        //    public static readonly SPDataSourceViewPropertyDescriptor FirstUniqueAncestor;
        //    public static readonly SPDataSourceViewPropertyDescriptor FirstUniqueAncestorWeb;
        //    public static readonly SPDataSourceViewPropertyDescriptor FirstUniqueRoleDefinitionWeb;
        //    public static readonly SPDataSourceViewPropertyDescriptor Folders;
        //    public static readonly SPDataSourceViewPropertyDescriptor Groups;
        //    public static readonly SPDataSourceViewPropertyDescriptor HasExternalSecurityProvider;
        //    public static readonly SPDataSourceViewPropertyDescriptor HasUniquePerm;
        //    public static readonly SPDataSourceViewPropertyDescriptor HasUniqueRoleAssignments;
        //    public static readonly SPDataSourceViewPropertyDescriptor HasUniqueRoleDefinitions;
        //    public static readonly SPDataSourceViewPropertyDescriptor ID;
        //    public static readonly SPDataSourceViewPropertyDescriptor IgnoreCheckOutLock;
        //    public static readonly SPDataSourceViewPropertyDescriptor IncludeSupportingFolders;
        //    public static readonly SPDataSourceViewPropertyDescriptor IsADAccountCreationMode;
        //    public static readonly SPDataSourceViewPropertyDescriptor IsADEmailEnabled;
        //    public static readonly SPDataSourceViewPropertyDescriptor IsParsingWebPartPage;
        //    public static readonly SPDataSourceViewPropertyDescriptor IsRootWeb;
        //    public static readonly SPDataSourceViewPropertyDescriptor Language;
        //    public static readonly SPDataSourceViewPropertyDescriptor LastItemModifiedDate;
        //    public static readonly SPDataSourceViewPropertyDescriptor Lists;
        //    public static readonly SPDataSourceViewPropertyDescriptor ListTemplates;
        //    public static readonly SPDataSourceViewPropertyDescriptor Locale;
        //    public static readonly SPDataSourceViewPropertyDescriptor MasterUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor MeetingCount;
        //    public static readonly SPDataSourceViewPropertyDescriptor MeetingInformation;
        //    public static readonly SPDataSourceViewPropertyDescriptor Modules;
        //    public static readonly SPDataSourceViewPropertyDescriptor Name;
        //    public static readonly SPDataSourceViewPropertyDescriptor ParentWeb;
        //    public static readonly SPDataSourceViewPropertyDescriptor ParentWebId;
        //    public static readonly SPDataSourceViewPropertyDescriptor Permissions;
        //    public static readonly SPDataSourceViewPropertyDescriptor PersonalizationMode;
        //    public static readonly SPDataSourceViewPropertyDescriptor PortalMember;
        //    public static readonly SPDataSourceViewPropertyDescriptor PortalName;
        //    public static readonly SPDataSourceViewPropertyDescriptor PortalSubscriptionUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor PortalUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor PresenceEnabled;
        //    public static readonly SPDataSourceViewPropertyDescriptor Properties;
        //    public static readonly SPDataSourceViewPropertyDescriptor PublicFolderRootUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor RecycleBin;
        //    public static readonly SPDataSourceViewPropertyDescriptor RegionalSettings;
        //    public static readonly SPDataSourceViewPropertyDescriptor ReusableAcl;
        //    public static readonly SPDataSourceViewPropertyDescriptor RoleAssignments;
        //    public static readonly SPDataSourceViewPropertyDescriptor RoleDefinitions;
        //    public static readonly SPDataSourceViewPropertyDescriptor Roles;
        //    public static readonly SPDataSourceViewPropertyDescriptor RootFolder;
        //    public static readonly SPDataSourceViewPropertyDescriptor SafeControls;
        //    public static readonly SPDataSourceViewPropertyDescriptor ServerNow;
        //    public static readonly SPDataSourceViewPropertyDescriptor ServerRelativeUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor Site;
        //    public static readonly SPDataSourceViewPropertyDescriptor SiteGroups;
        //    public static readonly SPDataSourceViewPropertyDescriptor SiteUserInfoList;
        //    public static readonly SPDataSourceViewPropertyDescriptor SiteUsers;
        //    public static readonly SPDataSourceViewPropertyDescriptor SyndicationEnabled;
        //    public static readonly SPDataSourceViewPropertyDescriptor Theme;
        //    public static readonly SPDataSourceViewPropertyDescriptor ThemeCssUrl;
        //    public static readonly SPDataSourceViewPropertyDescriptor Title;
        //    public static readonly SPDataSourceViewPropertyDescriptor TypeCache;
        //    public static readonly SPDataSourceViewPropertyDescriptor Url;
        //    public static readonly SPDataSourceViewPropertyDescriptor UserInfoListId;
        //    public static readonly SPDataSourceViewPropertyDescriptor UserIsSiteAdmin;
        //    public static readonly SPDataSourceViewPropertyDescriptor UserIsWebAdmin;
        //    public static readonly SPDataSourceViewPropertyDescriptor Users;
        //    public static readonly SPDataSourceViewPropertyDescriptor ViewMode;
        //    public static readonly SPDataSourceViewPropertyDescriptor ViewStyles;
        //    public static readonly SPDataSourceViewPropertyDescriptor Webs;
        //    public static readonly SPDataSourceViewPropertyDescriptor WebTemplate;
        //    public static readonly SPDataSourceViewPropertyDescriptor WebTemplateId;
        //}

        #endregion

        private ARollupDataSourceView dataSourceView;
        //private string[] fieldList;
        private DataRow resultItem;


        public RollupViewResultItem(ARollupDataSourceView dataSourceView, DataRow resultItem)
        {
            this.dataSourceView = dataSourceView;
            this.resultItem = resultItem;
            //this.fieldList = fieldList;
        }

        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties()
        {
            DataTable dt = resultItem.Table;
            DataColumnCollection columns = dt.Columns;
            RollupPropertyDescriptor[] arr = new RollupPropertyDescriptor[columns.Count];

            int c = 0;
            foreach (DataColumn column in columns)
            {
                Type t = column.DataType;
                if (column.ColumnName == "_ItemId")
                {
                    t = typeof (int);
                }

                if (column.ColumnName == "Modified")
                {
                    t = typeof(DateTime);
                }

                arr[c++] = new RollupPropertyDescriptor(column, t);                
            }

            PropertyDescriptorCollection coll = new PropertyDescriptorCollection(arr);
            return coll;
        }


        internal PropertyDescriptorCollection GetPropertyCollection()
        {
            return ((ICustomTypeDescriptor)this).GetProperties();
        }
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        AttributeCollection ICustomTypeDescriptor.GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        string ICustomTypeDescriptor.GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        string ICustomTypeDescriptor.GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        TypeConverter ICustomTypeDescriptor.GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        EventDescriptor ICustomTypeDescriptor.GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        PropertyDescriptor ICustomTypeDescriptor.GetDefaultProperty()
        {
              return TypeDescriptor.GetDefaultProperty(this, true);
        }
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        object ICustomTypeDescriptor.GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        EventDescriptorCollection ICustomTypeDescriptor.GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        EventDescriptorCollection ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }
       
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        PropertyDescriptorCollection ICustomTypeDescriptor.GetProperties(Attribute[] attribute)
        {
            return ((ICustomTypeDescriptor)this).GetProperties();
        }
        [SharePointPermission(SecurityAction.Demand, ObjectModel = true)]
        object ICustomTypeDescriptor.GetPropertyOwner(PropertyDescriptor descriptor)
        {
            return this;
        }

        // Properties
        public ARollupDataSourceView DataSourceView
        {
            get
            {
                return this.dataSourceView;
            }
        }

        public DataRow ResultItem
        {
            get
            {
                return this.resultItem;
            }
        }
        
    }
 

}
